{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_breast_cancer\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression  # 逻辑回归\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n逻辑回归算法  使用随机梯度下降  获取 回归系数；因此需要数据标准化\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "逻辑回归算法  使用随机梯度下降  获取 回归系数；因此需要数据标准化\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "keys dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename'])\n"
     ]
    }
   ],
   "source": [
    "cancer = load_breast_cancer()\n",
    "# 569样本  30个特征\n",
    "print(\"keys\", cancer.keys())\n",
    "\n",
    "# print(cancer.DESCR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取特征和标签\n",
    "X, y = cancer.data, cancer.target\n",
    "\n",
    "# print(\"标签\", cancer.target_names)  # 0恶性  1良性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X,\n",
    "                                                    y,\n",
    "                                                    test_size=0.2,\n",
    "                                                    stratify=y,\n",
    "                                                    random_state=1\n",
    "                                                    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "std = StandardScaler()\n",
    "std.fit(X_train)  # 在训练集在拟合【计算每列特征均值和标准差】\n",
    "X_train = std.transform(X_train)  # 【转换】\n",
    "X_test = std.transform(X_test)  # 【转换】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率 0.9912280701754386\n",
      "系数 [[-0.38556616 -0.36335414 -0.37181484 -0.41763823 -0.20488682  0.3411111\n",
      "  -0.75966014 -0.81296554  0.13172759  0.36051279 -1.17711803  0.03922804\n",
      "  -0.69492593 -0.83988347 -0.28529791  0.82440002  0.11073524 -0.33139564\n",
      "   0.28016896  0.59334263 -1.04444467 -1.07954518 -0.86083485 -0.98339718\n",
      "  -0.59639478 -0.10677702 -0.85919895 -0.81400765 -0.90975371 -0.47785707]]\n",
      "截距 [0.29510975]\n",
      "预测结果 [1 0 1 1 0 1 1 1 0 1 0 1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 0 1 0 0 0 0 0 0 1 0 1\n",
      " 0 0 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1\n",
      " 1 1 0 1 1 1 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 0 1 1 1 1 1 0\n",
      " 0 0 1]\n"
     ]
    }
   ],
   "source": [
    "alg = LogisticRegression()\n",
    "\n",
    "alg.fit(X_train, y_train)\n",
    "\n",
    "print(\"准确率\", alg.score(X_test, y_test))\n",
    "# print(\"测试集\", y_test.sum(), len(y_test))\n",
    "\n",
    "print(\"系数\", alg.coef_)\n",
    "print(\"截距\", alg.intercept_)\n",
    "\n",
    "print(\"预测结果\", alg.predict(X_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.091231   -0.14789362 -0.10871075 ...  0.18554767 -0.12121286\n",
      "   0.2904636 ]\n",
      " [ 0.63735495 -0.12672831  0.7264568  ...  0.86343024 -0.24509322\n",
      "   1.13641466]\n",
      " [-0.69022657  2.34726212 -0.68546322 ... -0.72308733 -0.06632915\n",
      "  -0.66764607]\n",
      " ...\n",
      " [ 0.91093466  0.64933333  0.84297245 ...  1.24473918  0.45428198\n",
      "  -0.48247855]\n",
      " [ 1.23347073 -0.17141064  1.13426158 ...  0.61981619  0.43860092\n",
      "  -0.81683819]\n",
      " [-0.44256494 -0.57825508 -0.50569622 ... -1.33605659 -1.58268748\n",
      "  -1.22103244]]\n"
     ]
    }
   ],
   "source": [
    "# (114, 30)*(1, 30)\n",
    "# print(\"预测结果_自己实现\", (X_test*alg.coef_).sum(axis=1) + alg.intercept_)\n",
    "# print(\"decision_function\", alg.decision_function(X_test))\n",
    "# print(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
